#include<iostream> 
#include "stdlib.h"

using namespace std;

void duplication(int arr[] , int n){
	cout << "start... "<< endl;
	int i = 0;
	while(i < n){
		int index = arr[i];
		// 判断下标key是否与value相等, 相等i++，
		if ( index != i){
			int tmp = arr[index];
			if ( tmp == index){
				cout << "重复数字: " << index << endl;
				break;	
			}else {
				// 交换
				arr[i] = tmp;
				arr[index] = index;
			} 
		}else {
			i++;
		}
	}
}



int duplicationPosive(int arr[] , int n){
	for ( int i =0; i<n; i++){
		while ( i != arr[i] ){
			if ( arr[i] == arr[arr[i]] ){
				return arr[i];
			}
			int tmp = arr[i];
			arr[i] = arr[arr[i]];
			arr[tmp] = tmp;
		}
	}
	return -1;
}



void print(int arr[], int n){
	for (int i=0; i< n; i++){
		cout << arr[i] << endl;
	}
}



int main(void) {
	int arr[] = {2,3,1,0,2,5,3};
	int tmp = duplicationPosive(arr, 7);
	cout << "重复数字: " << tmp << endl;
	cout << "---------" << endl;
	print(arr, 7);
	return 0;
}